<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Asset extends Model
{
    protected $dateFormat = 'U';

    protected $fillable = ['trade_queue_id','amount','end_time'];


    // 活的波比
    public function sunLine()
    {
        $user = auth('api')->user();
        $baseUser = User::withDepth()->find($user->id); // 获取 depth
        //return $baseUser;

        // 7 层分佣 == 太阳线
        for ($i=0;$i<7;$i++) {
            if($baseUser->parent_id) {
                $ancestor = User::withDepth()->find($baseUser->parent_id); // 获取 depth
                $this->createAncestorProfit($ancestor);
                $baseUser = $ancestor;
            }else {
                break;
            }
        }

    }

    // 目前也是做成了数据库
    public function globalBonusPool()
    {
        $baseUser = auth('api')->user();

        while ($baseUser->parent_id) {
            $ancestor = User::find($baseUser->parent_id); // 获取 depth
            $this->createAncestorGlobalProfit($ancestor);
            $baseUser = $ancestor;
        }
        /* 100 代
        for ($i=0;$i<100;$i++) {
            if($baseUser->parent_id) {
                $ancestor = User::find($baseUser->parent_id); // 获取 depth
                $this->createAncestorGlobalProfit($ancestor);
                $baseUser = $ancestor;
            }else {
                break;
            }
        }
        */
    }
    public function createAncestorGlobalProfit($ancestor)
    {
        $count = User::where('parent_id',$ancestor->id)->count();        // 直推统计
        $teamCount = count($ancestor->descendants()->pluck('id'));        // 团队统计

        $globalBonusPoolRate = GlobalBonusPoolRate::orderBy('rate','desc')->get();
        foreach ($globalBonusPoolRate as $k=>$v) {
            $sunLineProfit = bcmul($this->amount,$v->rate,10);
            $referenceRate = TeamProfitRate::where('type','reference')->value('rate');
            $shopRate = TeamProfitRate::where('type','shop')->value('rate');
            $referenceAmountProfit = bcmul($sunLineProfit,$referenceRate,10);
            $shopAmountProfit = bcmul($sunLineProfit,$shopRate,10);
            if($count > $v->direct_pushing_number && $teamCount > $v->team_number) {
                $ancestor->increment('reference_amount',$referenceAmountProfit);
                $ancestor->increment('shop_amount',$shopAmountProfit);
                break;
            }
        }
    }

    // 父级分佣
    public function createAncestorProfit($ancestor)
    {
        $user = auth('api')->user();
        $baseUser = User::withDepth()->find($user->id); // 获取 depth

        $depthOffset = \bcsub($baseUser->depth, $ancestor->depth);

        $count = User::where('parent_id',$ancestor->id)->count();        // 直推统计
        //return $count;
        $SunLineRate = SunLineRate::where('level',$depthOffset)->first(); // 比率

        if(!$SunLineRate) {
            return;
        }
        //return $depthOffset;
        switch ($depthOffset)
        {
            case '1':
                //return $SunLineRate->direct_pushing_number;
                if($count < $SunLineRate->direct_pushing_number) {
                    return;
                }
            case '2':
                if($count < $SunLineRate->direct_pushing_number) {
                    return;
                }
            case '3':
                if($count < $SunLineRate->direct_pushing_number) {
                    return;
                }
                break;
            case '4':
                if($count < $SunLineRate->direct_pushing_number) {
                    return;
                }
                break;
            case '5':
                if($count < $SunLineRate->direct_pushing_number) {
                    return;
                }
                break;
            case '6':
                if($count < $SunLineRate->direct_pushing_number) {
                    return;
                }
                break;
            case '7':
                if($count < $SunLineRate->direct_pushing_number) {
                    return;
                }
                break;
            default:
                return;
        }

        $sunLineProfit = bcmul($this->amount,$SunLineRate->rate,10);
        // 团队奖收益的10%换粉粉链只能用于购物
        $referenceRate = TeamProfitRate::where('type','reference')->value('rate');
        $shopRate = TeamProfitRate::where('type','shop')->value('rate');

        $referenceAmountProfit = bcmul($sunLineProfit,$referenceRate,10);
        $shopAmountProfit = bcmul($sunLineProfit,$shopRate,10);

        $ancestor->increment('reference_amount',$referenceAmountProfit);
        $ancestor->increment('shop_amount',$shopAmountProfit);
    }
    
}
